<?php

namespace frontend\modules\shop\controllers;

use Yii;
use common\helpers\Url;
use frontend\components\Controller;
use common\helpers\ArrayHelper;
use common\models\shop\ShopCart;
use frontend\modules\shop\models\CartSearch;
use frontend\modules\shop\models\CartForm;
use frontend\modules\shop\models\CartPrepareForm;
use frontend\modules\shop\models\CartPackSearch;
use common\logics\DiscountLogic;
use common\logics\FrontUserAddressLogic;

/**
 * 购物车相关接口
 * 
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class CartController extends Controller {

    /**
     * @inheritdoc
     */
    protected function rules() {
        return [
            [
                'allow' => true,
                'roles' => ['@'],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    protected function verbs() {
        return [
            'add' => ['POST'],
            'delete' => ['POST'],
        ];
    }

    /**
     * 购物车列表
     * @return mixed
     */
    public function actionIndex() {
        $user_id = $this->uid;
        ShopCart::updateAll(['status' => ShopCart::STATUS_CHECKED], [
            'user_id' => $user_id,
            'status' => ShopCart::STATUS_PREPARE_ORDER
        ]);
        /* @var $model \frontend\modules\shop\models\CartSearch */
        $model = new CartSearch();
        $model->user_id = $user_id;
        return $model->search();
    }

    /**
     * 加入购物车
     * @return json
     */
    public function actionAdd() {
        $model = new CartForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            return $model->dumpError('加入购物车失败');
        }
        $model->user_id = $this->uid;
        return $model->add();
    }

    /**
     * 加入购物车
     * @return json
     */
    public function actionChange($id, $num) {
        $model = new CartForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            return $model->dumpError('加入购物车失败');
        }
        $model->user_id = $this->uid;
        return $model->save();
    }

    /**
     * 结算提交保存
     */
    public function actionBalance() {
        $model = new CartPrepareForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            return $model->dumpError('购物车商品结算提交失败');
        }
        $model->user_id = $this->uid;
        return $model->save();
    }

    /**
     * 购物车物品打包结算
     * @return mixed
     */
    public function actionPack() {
        $list = CartPackSearch::waitBalance($this->uid);

        $dislogic = new DiscountLogic($list);
        $goods = $dislogic->reckon();

        $amount = 0;
        $discount = 0;
        foreach ($goods as $ginfo) {
            $_amount = $ginfo['price'] * $ginfo['number'];
            $_discount = $_amount - $ginfo['subtotal'];
            $amount += $_amount;
            $discount += $_discount;
        }

        $addresslist = FrontUserAddressLogic::findByUserId($this->uid);
        $chosenAddressId = ArrayHelper::getValue($addresslist, [0, 'id'], 0);

        return [
            'list' => $list,
            'order' => [
                'amount' => $amount,
                'discount' => $discount,
                'subtotal' => $amount - $discount,
                'express' => 0,
            ],
            'chosenAddressId' => $chosenAddressId,
            'address' => [
                'list' => $addresslist,
                'url' => Url::h5('/user/address'),
            ],
        ];
    }

    /**
     * 批量删除购物车商品
     */
    public function actionDelete() {
        $ids = Yii::$app->request->post('ids');
        if (!empty($ids)) {
            ShopCart::deleteAll([
                'user_id' => $this->uid,
                'id' => $ids
            ]);
        }
        return true;
    }

    /**
     * 删除单个购物车商品
     */
    public function actionRemove($id) {
        $model = ShopCart::findOrFail([
            'id' => $id,
            'user_id' => $this->uid
        ]);
        return $model->delete() !== false;
    }

}
